package com.cy.common.config;


import java.util.LinkedHashMap;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cy.sys.service.realm.SysUserRealm;

/**
 * 此类配置Shiro的依赖
 * @author private
 *
 */
@Configuration
public class SpringShiroConfig {
	
	/**
	 * 配置检测登录和权限的对象
	 * @return
	 */
	@Bean
	public Realm realm() {
		SysUserRealm realm = new SysUserRealm();
		return realm;
	}
	
	/**
	 * 配置资源拦截方式
	 * @return
	 */
	@Bean
	public ShiroFilterChainDefinition shiroFilterChainDefinition() {
		DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
	    LinkedHashMap<String, String> map = new LinkedHashMap<>();
	    //先设置需要放行什么资源
	    map.put("/bower_components/**", "anon");
	    map.put("/build/**", "anon");
	    map.put("/dist/**", "anon");
	    map.put("/plugins/**", "anon");
	    map.put("/user/doLogin", "anon");
	    map.put("/timg.jpg", "anon");
	    map.put("/kaptcha", "anon");
	    map.put("/doLogout", "logout");
	    //最后设置剩下的资源拦截条件
	    map.put("/**", "user");
	    //将条件添加到chainDefinition中
	    chainDefinition.addPathDefinitions(map);
	    return chainDefinition;
	}
	
	/**
	 * 配置缓存
	 * @return
	 */
	@Bean
	protected CacheManager ahiroCacheManager() {
	    return new MemoryConstrainedCacheManager();
	}
}
